// pages/pay/pay.js
const util = require('../../utils/util')
const  db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    box:[],
    money:0,
    paper_box_name:"",
    phone_number:"",
    address:"",
    beizhu:"",
    yunfei:10,
    box_id:"",
    box_src:"",
    box_price:"",
    box_name:"",
    box_size:"",
    box_material:"",
    box_num:"",
    judge_state:"",
  },
  // 备注
  beizhu:function(e){
    let that = this
    console.log(e)
    that.setData({
      beizhu:e.detail.value
    })
  },

  // onClickCar:function(e){
  //   let that=this
    
   

  // },

  // 结算
  pay:function(e){
    let that = this
    // that.onClickCar()
    var DATE = util.formatDate(new Date());
    
      for(var x=0;x<that.data.box.length;x++){
        if(that.data.box[x].box_checked=="true"){
          that.setData({
            address:that.data.box[x].activity_location,
            paper_box_name:that.data.box[x].paper_box_name,
            box_src:that.data.box[x].box_src,
            box_price:that.data.box[x].paper_box_price,
            
            // box_area:that.data.box_area,
            // paper_box_ad:that.data.paper_box_ad,
            box_size:that.data.box[x].paper_box_size,
            box_material:that.data.box[x].paper_box_material,
            
            // box_id:that.data.id
          })
          
        }
      }
            // &&that.data.phone_number!==""
      if(that.data.paper_box_name!==""&&that.data.address!==""&&that.data.beizhu!==""){
      console.log('测试'+that.data.paper_box_name)
      db.collection('order').add({
            data:{
              box_name:that.data.paper_box_name,
              phone_number:that.data.phone_number,
              address:that.data.address,
              box_src:that.data.box_src,
              box_price:that.data.box_price,
             
              // box_area:that.data.box_area,
              // paper_box_ad:that.data.paper_box_ad,
              box_size:that.data.box_size,
              box_material:that.data.box_material,
              beizhu:that.data.beizhu,
              money:that.data.money,
              box:that.data.box,
              time:DATE,
              box_state:"送货中",
              judge_state:"待评价"
            },success:function(res){
              console.log('下单成功',res)
              wx.cloud.callFunction({
                name:"product_delet",
                data:{
                },
                success:function(res){
                  console.log('购物车删除成功',res)
                  for(var i= 0;i<that.data.box.length;i++){
                    wx.cloud.callFunction({
                      paper_box_name:"inc_box_num",
                      data:{
                        box_id:that.data.box[i].box_id
                      },success:function(res){
                        console.log('商品销量自加成功',res)
                      }
                    })
                  }
                  wx.navigateBack({
                    url: '../shopping_car/shopping_car',
                  })
                },fail:function(res){
                  console.log('购物车删除失败',res)
                }
              })
            },fail:function(res){
              console.log('下单失败',res)
            }
          })
    }else{
      wx.showToast({
        title: '请填写完整信息',
        icon:"none"
      })
    }
    
  },
  // 选择地址
  // address:function(e){
  //   let that = this
  //   wx.getSetting({
  //     success(res) {
  //       if (res.authSetting['scope.address']) {
  //         wx.authorize({
  //           scope: 'scope.address',
  //           success () {
  //             wx.chooseAddress({
  //               success (res) {
  //                 console.log(res)
  //                 that.setData({
  //                   paper_box_name:res.userName,
  //                   phone_number:res.telNumber,
  //                   address:res.provinceName+res.cityName+res.countyName+res.detailInfo
  //                 })
  //               }
  //             })
  //           }
  //         })
  //       }else{
  //         wx.openSetting({
  //           success (res) {
  //             console.log(res.authSetting)
  //           }
  //         })
  //       }
  //     }
  //   })
  // },
   // 计算总金额
   get_money_sum(){
    let that=this
    let money_sum = 0
    for(var x=0;x<that.data.box.length;x++){
      if(that.data.box[x].box_checked == "true"){
        money_sum=money_sum+(that.data.box[x].paper_box_num*that.data.box[x].paper_box_price)
      }
    }
    that.setData({
      money:money_sum
    })

  },


  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    
    db.collection('shopping_car').where({
      box_checked:"true"
    }).get({
      success:function(res){
        
        that.setData({
          box:res.data           
        })
        that.get_money_sum()
      },fail:function(res){
        console.log('获取商品失败',res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})